package com.laowang.project.pay.config;


import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WxMsgPushNew {


    /**
     * 微信公众号API的Service
     */
    //@Autowired
    private final WxMpService wxMpService;

    /**
     * 构造注入
     */
    public WxMsgPushNew(WxMpService wxMpService) {
        this.wxMpService = wxMpService;
    }

    /**
     * 发送微信模板信息  模板1
     *
     * @param openId 接受者openId
     * @return 是否推送成功
     */
    public Boolean SendWxMsg(String openId, ReceiveOrderVo receiveOrderVo) {
        // 发送模板消息接口
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
            // 接收者openid
            .toUser(openId)
            // 模板id
            .templateId(WeixinOpenNew.templateId)
            // 模板跳转链接   h5 的地址
            //.url("pages/admin-order-list/order-list")
            .miniProgram(new WxMpTemplateMessage.MiniProgram(WxConfig.APP_ID, "pages/index/index", false))
            .build();
        // 添加模板数据
    /*if (receiveOrderVo.getUrl() != null){
      templateMessage.setUrl(receiveOrderVo.getUrl());
    }*/
        templateMessage.addData(new WxMpTemplateData("thing3", receiveOrderVo.getThing3(), "#FF00FF")) //商品名称
            .addData(new WxMpTemplateData("time4", receiveOrderVo.getTime4(), "#A9A9A9")) //下单时间
            .addData(new WxMpTemplateData("amount5", receiveOrderVo.getAmount5(), "#FF00FF"))//支付金额
            .addData(new WxMpTemplateData("number9", receiveOrderVo.getNumber9(), "#000000"));//会员号码
        String msgId = null;
        try {
            // 发送模板消息
            msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        log.warn("·==++--·推送微信模板信息：{}·--++==·", msgId != null ? "成功" : "失败");
        return msgId != null;
    }


    /**
     * 发送微信模板信息  模板1
     *
     * @param openId 接受者openId
     * @return 是否推送成功
     */
    public Boolean SendWxMsg1(String openId, ReceiveOrderVo1 receiveOrderVo1) {
        // 发送模板消息接口
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
            // 接收者openid
            .toUser(openId)
            // 模板id
            .templateId(WeixinOpenNew.templateId1)
            // 模板跳转链接   h5 的地址
            //.url("https://www.zhugongjia.cn/header/index.html")
            .miniProgram(new WxMpTemplateMessage.MiniProgram(WxConfig.APP_ID, "pages/index/index", false))
            .build();
        // 添加模板数据
              /*  {{first.DATA}}
                申请人：{{keyword1.DATA}}
                申请时间：{{keyword2.DATA}}
                申请状态：{{keyword3.DATA}}
                {{remark.DATA}}*/
        templateMessage.addData(new WxMpTemplateData("amount1", receiveOrderVo1.getAmount1(), "#FF00FF"))
            .addData(new WxMpTemplateData("time3", receiveOrderVo1.getTime3(), "#A9A9A9"))
            .addData(new WxMpTemplateData("thing11", receiveOrderVo1.getThing11(), "#FF00FF"));

        String msgId = null;
        try {
            // 发送模板消息
            msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        log.warn("·==++--·推送微信模板信息：{}·--++==·", msgId != null ? "成功" : "失败");
        return msgId != null;
    }


    public Boolean SendWxMsg2(String openId, ReceiveOrderVo2 receiveOrderVo2) {
        // 发送模板消息接口
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
            // 接收者openid
            .toUser(openId)
            // 模板id
            .templateId(WeixinOpenNew.templateId2)
            // 模板跳转链接   h5 的地址
            //.url("https://www.zhugongjia.cn/header/index.html")
            .miniProgram(new WxMpTemplateMessage.MiniProgram(WxConfig.APP_ID, "pages/index/index", false))
            .build();
        // 添加模板数据
              /*  {{first.DATA}}
                申请人：{{keyword1.DATA}}
                申请时间：{{keyword2.DATA}}
                申请状态：{{keyword3.DATA}}
                {{remark.DATA}}*/
        templateMessage.addData(new WxMpTemplateData("character_string1", receiveOrderVo2.getCharacter_string1(), "#FF00FF"))
            .addData(new WxMpTemplateData("thing2", receiveOrderVo2.getThing2(), "#A9A9A9"))
            .addData(new WxMpTemplateData("time5", receiveOrderVo2.getTime5(), "#FF00FF"));

        String msgId = null;
        try {
            // 发送模板消息
            msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        log.warn("·==++--·推送微信模板信息：{}·--++==·", msgId != null ? "成功" : "失败");
        return msgId != null;
    }


    /**
     *  申请俱乐部提醒
     * @param openId
     * @param receiveOrderVo2
     * @return
     */
    public Boolean SendWxMsg3(String openId, ReceiveOrderVo3 receiveOrderVo2) {
        // 发送模板消息接口
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
            // 接收者openid
            .toUser(openId)
            // 模板id
            .templateId(WeixinOpenNew.templateId3)
            // 模板跳转链接   h5 的地址
            //.url("https://www.zhugongjia.cn/header/index.html")
            .miniProgram(new WxMpTemplateMessage.MiniProgram(WxConfig.APP_ID, "pages/index/index", false))
            .build();
        /*详细内容

            用户昵称  {{phrase10.DATA}}
        申请时间{{time5.DATA}}
        联系电话{{phone_number13.DATA}}
        报名类型{{thing2.DATA}}

        基础信息

            模板ID
            e5Rgz7HPq6umcEvP8C3hmrVJykkPJKX9ybal-k7bjW4
            模板编号
            46046
            标题
            收到报名申请通知
            所属类目
            预约/报名
            操作人
            一世****浮沉 2024-05-27 添加
            场景说明
            报名申请

        */
        templateMessage.addData(new WxMpTemplateData("phrase10", receiveOrderVo2.getPhrase10(), "#FF00FF"))
            .addData(new WxMpTemplateData("phone_number13", receiveOrderVo2.getPhone_number13(), "#A9A9A9"))
            .addData(new WxMpTemplateData("time5", receiveOrderVo2.getTime5(), "#FF00FF"))
            .addData(new WxMpTemplateData("thing2", receiveOrderVo2.getThing2(), "#FF00FF"))
            .addData(new WxMpTemplateData("time5", receiveOrderVo2.getTime5(), "#FF00FF"));

        String msgId = null;
        try {
            // 发送模板消息
            msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        log.warn("·==++--·推送微信模板信息：{}·--++==·", msgId != null ? "成功" : "失败");
        return msgId != null;
    }
}
